//
//  SetPasswordView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI

struct AiSetVoiceView: View {
    var forceShow = false
    @EnvironmentObject var appStorageManager: AppStorageManager
    @ObservedObject var viewModel : AiSetViewModel
    @State private var showVoice = false
    
    @State var isShow = false
    
    var body: some View {
        HStack{
            VStack(alignment: .leading, spacing: 18) {
                HStack{
                    Text("语音设置")
                        .foregroundColor(.bgItemContent)
                        .fontWeight(.bold)
                        .font(.system(size: 14))
                    Spacer()
                    Image(.myIconExpand)
                        .resizable()
                        .frame(width: 15,height: 15)
                        .rotationEffect(isShow ? Angle(degrees: 180) : .zero)
                }.contentShape(Rectangle())// 将整个矩形区域（包括padding）都设置为可点击区域
                    .onTapGesture {
                        withAnimation(Animation.easeInOut(duration: 0.3)) {
                            if !forceShow {
                                isShow.toggle()
                            }
                        }
                    }
                
                if isShow{
                    Toggle("语音来电", isOn: appStorageManager.$toggleAivoice)
                        .foregroundColor(.bgItemContent)
                        .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
                        .font(.system(size: 14))
                        .onChange(of: appStorageManager.toggleAivoice) { newValue in
                            TaskCallExt.instance.updateTask()
                        }
                    Toggle("震动开关", isOn: appStorageManager.$toggleAiVibrate)
                        .foregroundColor(.bgItemContent)
                        .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
                        .font(.system(size: 14))
                    HStack {
                        Text("来电偏好")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        Spacer()
                        Text("\(viewModel.getTimePeriod(viewModel.callTimeStart))-\(viewModel.getTimePeriod(viewModel.callTimeEnd))")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture{
                            showStartTimePopup()
                        }
                    HStack {
                        Text("免费音色次数")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        Spacer()
                        Text("\(userManager.getExtraConfig().aiVoiceClone ?? 0)")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            showVoice.toggle()
                        }
                    HStack {
                        Text("免费制作音色次数")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        Spacer()
                        Text("\(userManager.getExtraConfig().aiVoiceCreate ?? 0)")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            showVoice.toggle()
                        }
//                    HStack {
//                        Text("自定义铃声")
//                            .foregroundColor(.bgItemContent)
//                            .font(.system(size: 14))
//                        Spacer()
//                        Text("系统")
//                            .foregroundColor(.bgNormalText4)
//                            .font(.system(size: 10))
//                        Image(.appIconArrowRight)
//                            .resizable()
//                            .frame(width: 15,height: 15)
//                        
//                    }.padding(.top,5).padding(.bottom,5)
                    HStack {
                        Text("点击语音试听    ")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        Spacer()
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            viewModel.doVoiceTest()
                        }
                }
            }
            Spacer()
        }.frame(maxWidth: .infinity).padding(18)
            .background(.bgItem)
            .clipShape(RoundedRectangle(cornerRadius: 15))
            .onAppear{
                if !viewModel.isInit {
                    viewModel.getTimeData()
                    viewModel.isInit = true
                }
                if forceShow {
                    isShow = true
                }
            }
            .sheet(isPresented: $showVoice) {
                //语音选择试图
                AiVoiceChooseView() { _ in}.presentationDragIndicator(.visible)
            }
    }
    
    func showStartTimePopup() {
        SwiftEntryKitWrapper.showBottomPopup { dissmis in
            PickTimeView(title:"请选择开始时间", time: $viewModel.callTimeStart) {
                dissmis()
                showEndTimePopup()
            }
        }
    }
    
    func showEndTimePopup(){
        SwiftEntryKitWrapper.showBottomPopup { dissmis in
            PickTimeView(title:"请选择开始时间", time: $viewModel.callTimeEnd) {
                dissmis()
                viewModel.doSaveCallTime()
            }
        }
    }
}

//#Preview {
//    SetPasswordView()
//}

//定义预览提供者
struct AiSetVoice_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: AiSetVoiceView(viewModel: AiSetViewModel(), isShow: true))
    }
}
